using System.Threading;
using System;

namespace Kioskvalt.Server
{
    class SeqThread
    {
        bool keeprunning;
        Thread t;

        public void Start()
        {
            keeprunning = true;
            ThreadStart ts = new ThreadStart(Run);
            t = new Thread(ts);
            t.Priority = ThreadPriority.Normal;
            t.Start();
        }	

        public void Stop()
        {
            keeprunning = false;
            t.Abort();
        }

        public void Run()
        {
#if WIN32
            Win32.HiPerfTimer2 timer = new Win32.HiPerfTimer2();
            timer.Start();
            double lasttimer = timer.DeltaTime;
            while (keeprunning)
            {
                double t = timer.DeltaTime;
                double dT = t - lasttimer;
                lasttimer = t;
                State.Sequencer.TimerTick(dT);
                Thread.Sleep(5);
            }

            timer.Stop();
#else

		// legacy crappy timer
			// DateTime dt0 = DateTime.Now;
            double lasttimer = (double)DateTime.Now.Ticks;
            while (keeprunning)
            {
                double t = (double)DateTime.Now.Ticks;
                double dT = (t - lasttimer) / 1000.0	;
                lasttimer = t;
                State.Sequencer.TimerTick(dT);
                Thread.Sleep(15);
            }

#endif
			
		}
    }
}
